<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动名称" prop="actName">
        <el-input
          v-model="queryParams.actName"
          placeholder="请输入活动名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:CompassActInfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:CompassActInfo:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" @cell-dblclick="cellDbClick" :data="CompassActInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="活动名称" align="center" prop="actName"/>
      <el-table-column label="截止报名时间" align="center" prop="endRegistration"/>
      <el-table-column label="排序" align="center" prop="sort">
        <template #default="{row}">
          <el-input type="number" v-model="row.sort" @blur="saveSort(row)" autocomplete="on" ref="input"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="actFlag"/>
      <el-table-column label="是否精选" align="center" prop="isChosen">
        <template #default="{row}">
          <span>{{row.isChosen ? '是' : '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:CompassActInfo:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:CompassActInfo:remove']"
          >删除
          </el-button>
          <router-link :to="{name: 'ActUsers', query: {id: scope.row.id, name: scope.row.actName}}" style="margin-left: 10px">
            <el-button
              size="mini"
              type="text"
            >报名详情</el-button>
          </router-link>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateIsChoosen(scope.row)"
            v-hasPermi="['system:CompassVideoEva:edit']"
          >{{!scope.row.isChosen ? '设置精选' : '取消精选'}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改线下活动详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body @close="clearEditor">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="活动名称" prop="actName">
          <el-input v-model="form.actName" placeholder="请输入活动名称"/>
        </el-form-item>
        <el-form-item label="截止报名时间" prop="endRegistration">
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="form.endRegistration"
            type="datetime">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="endTime">
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="form.timeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="活动开始时间"
            end-placeholder="活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-row>
          <el-col :span="14">
            <el-form-item label="活动地点" prop="actAddress">
              <el-input v-model="form.actAddress" placeholder="请输入活动地点"/>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="纬度" prop="latitude" label-width="60px">
              <el-input v-model="form.latitude" placeholder="纬度"/>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="经度" prop="longitude" label-width="60px">
              <el-input v-model="form.longitude" placeholder="经度"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首页封面" prop="indexImage">
  <!--            <el-input v-model="form.indexImage" placeholder="请输入首页图片"/>-->
              <Upload v-model="form.indexImage" @clear="clearImageValidate" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动封面" prop="listImage">
  <!--            <el-input v-model="form.listImage" placeholder="请输入列表图片"/>-->
              <Upload v-model="form.listImage" @clear="clearImageValidate" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="活动内容" prop="actDesc">
          <Editor v-model="form.actDesc" ref="editor"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompassActInfo,
  getCompassActInfo,
  delCompassActInfo,
  addCompassActInfo,
  updateCompassActInfo,
  exportCompassActInfo,
  updateChosenFlag
} from '@/api/system/CompassActInfo'
import Editor from "@/components/Editor"
import Upload from "@/components/Upload"

export default {
  name: 'CompassActInfo',

  components: {
    Editor,
    Upload
  },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线下活动详情表格数据
      CompassActInfoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        actName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        actName: [
          { required: true, message: '活动名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    clearEditor() {
      this.$refs.editor.clearEditor()
    },

    handleUpdateIsChoosen(row) {
      updateChosenFlag({
        id: row.id,
        flag: row.isChosen ? 0 : 1
      }).then(res => {
        row.isChosen = !row.isChosen
        this.msgSuccess('操作成功')
      })
    },

    saveSort(row) {
      row.edit = false
      const postData = {
        ...row,
        actDesc: this.filterContent(row.actDesc)
      }
      updateCompassActInfo(postData).then(response => {
        if (response.code === 200) {
          this.msgSuccess('修改成功')
        }
      })
    },

    cellDbClick(row, column) {
      if (column.property === 'sort') {
        this.$set(row, 'edit', true)
        this.$nextTick(() => {
          this.$refs.input.focus()
        })
      }
    },

    clearImageValidate() {
      this.$refs["form"].clearValidate("indexImage");
      this.$refs["form"].clearValidate("listImage");
    },
    /** 查询线下活动详情列表 */
    getList() {
      this.loading = true
      listCompassActInfo(this.queryParams).then(response => {
        this.CompassActInfoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        endRegistration: null,
        actName: null,
        endTime: null,
        actAddress: null,
        actFlag: null,
        actDesc: null,
        indexImage: null,
        isChosen: null,
        startTime: null,
        listImage: null,
        sort: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加线下活动详情'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCompassActInfo(id).then(response => {
        response.data.timeRange = [response.data.startTime, response.data.endTime]
        this.form = response.data
        this.open = true
        this.title = '修改线下活动详情'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (this.form.timeRange.length) {
          this.form.startTime = this.form.timeRange[0]
          this.form.endTime = this.form.timeRange[1]
        }

        const postData = {
          ...this.form,
          actDesc: this.filterContent(this.form.actDesc)
        }

        if (valid) {
          if (this.form.id != null) {
            updateCompassActInfo(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addCompassActInfo(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除线下活动详情编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCompassActInfo(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有线下活动详情数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportCompassActInfo(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>
